package cn.bopomo.dealer.base.util;

import java.util.ArrayList;
import java.util.List;

public class PageList {
	private int currentPage = 0;
	private int startRow = 0;
	private int endRow = 0;
	private int firstPage = 0;
	private int nextPage = 0;
	private int prevPage = 0;
	private int lastPage = 0;
	private int totalPage = 0;
	private long totalRecord = 0;
	private int pageSize = 10;
	private List curPageList;
	private int navBarSize = 5;
	private int nextNavBar = 0;
	private int prevNavBar = 0;
	private List navBarList;
	
	private PageList(){}
	
	public PageList(int currentIndex){
		changeCurrentPage(currentIndex);
	}
	
	public PageList(int currentIndex, int pageSize){
		if(pageSize > 0)
			this.pageSize = pageSize;
		changeCurrentPage(currentIndex);
	}
	
	public PageList(int currentIndex, int pageSize, int navBarSize){
		if(pageSize > 0)
			this.pageSize = pageSize;
		if(navBarSize > 0)
			this.navBarSize = navBarSize;
		changeCurrentPage(currentIndex);
	}
	
	public int getNavBarSize() {
		return navBarSize;
	}
	
	public int getNextNavBar() {
		return nextNavBar;
	}

	public int getPrevNavBar() {
		return prevNavBar;
	}

	public List getNavBarList() {
		return navBarList;
	}

	public List getCurPageList() {
		return curPageList;
	}
	public void setCurPageList(List curPageList) {
		this.curPageList = curPageList;
	}
	public int getCurrentPage() {
		return currentPage+1;
	}

	public int getNextPage() {
		return nextPage;
	}
	
	public int getPageSize() {
		return pageSize;
	}
	
	public int getPrevPage() {
		return prevPage;
	}
	
	public int getTotalPage() {
		return totalPage;
	}
	
	public long getTotalRecord() {
		return totalRecord;
	}
	public void setTotalRecord(int totalRecord) {
		if(totalRecord > 0){
			this.totalRecord = totalRecord;
			if(pageSize > 0){
				int indexPage = currentPage+1;
				calTotalPage(indexPage);
				changeNavBar(indexPage);
			}
		}
	}
	
	
	private void changeCurrentPage(int currentPage) {
		if(currentPage > 0){
			this.currentPage = currentPage-1;
			nextPage = currentPage+1;
			prevPage = currentPage-1;
			if(currentPage == 1)
				prevPage = currentPage;
			else if(currentPage>=totalPage && totalPage>0)
				nextPage = totalPage;
			
			int temp = this.currentPage * pageSize;
			startRow = temp;
			endRow = temp + (this.currentPage==totalPage && (totalRecord%pageSize)!=0?(int)(totalRecord%pageSize):pageSize);
		}
	}
	
	
	private void calTotalPage(int indexPage){
		int _totalPage = (int)(totalRecord % pageSize);
		totalPage = (int)(totalRecord / pageSize);
		if(_totalPage > 0)
			totalPage = totalPage + 1;
		
		if(indexPage >= totalPage)
			changeCurrentPage(totalPage);
	}
	
	
	private void changeNavBar(int indexPage){
		/*
		 * 计算当前页位于那个区间，区间从0开始算起
		 */
		int _interzone = indexPage % navBarSize;
		int interzone = indexPage / navBarSize;
		if(_interzone == 0)
			interzone--;
		/*
		 * 计算当前区间内的页数
		 */
		int _totalInterZone = totalPage % navBarSize;
		int totalInterZone = totalPage / navBarSize;
		if(_totalInterZone == 0)
			totalInterZone--;
		
		int times = navBarSize;
		int _temp = interzone*navBarSize;
		if( interzone == totalInterZone)
			times = totalPage-_temp;
		
		
		navBarList = new ArrayList();
		for(int i=1; i<=times; i++){
			navBarList.add(new Integer(_temp+i));
		}
		
		/*
		 * 计算两边的边界
		 */
		nextNavBar = times==navBarSize?_temp+navBarSize+1:totalPage;
		prevNavBar = interzone==0?1:_temp;
	}

	public int getEndRow() {
		return endRow;
	}

	public int getStartRow() {
		return startRow;
	}
}
